package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.PaOrder;
import com.ruoyi.system.domain.PaOrderItem;
import com.ruoyi.system.domain.PaUserAddrOrder;
import com.ruoyi.system.domain.bo.PaOrderItemBo;
import com.ruoyi.system.domain.bo.SupplierOrderBo;
import com.ruoyi.system.domain.bo.SupplierUpdateOrderBo;
import com.ruoyi.system.domain.vo.SupplierOrderVo;
import com.ruoyi.system.mapper.PaOrderItemMapper;
import com.ruoyi.system.mapper.PaOrderMapper;
import com.ruoyi.system.mapper.PaUserAddrOrderMapper;
import com.ruoyi.system.service.ISupplierOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName SupplierOrderServiceImpl
 * @Description 供应商服务
 * @date 2024/7/4 19:29
 * @Version 1.0
 * @Auther: zhangzhenyu
 */
@Service
@Slf4j
public class SupplierOrderServiceImpl implements ISupplierOrderService {
    @Autowired
    private PaOrderMapper paOrderMapper;

    @Autowired
    private PaOrderItemMapper paOrderItemMapper;

    @Autowired
    private PaUserAddrOrderMapper addrOrderMapper;
    @Override
    public TableDataInfo<SupplierOrderVo> selectPaOrderList(SupplierOrderBo supplierOrderBo) {
        List<SupplierOrderVo> supplierOrderVos = new ArrayList<>();
        PaOrder paOrder = new PaOrder();
        BeanUtils.copyBeanProp(paOrder, supplierOrderBo);
        PageUtils.startPage();
        List<PaOrder> paOrders = paOrderMapper.selectPaOrderList(paOrder);
        PageInfo<PaOrder> pageInfo = PageInfo.of(paOrders);
        for (PaOrder paOrder1 : paOrders) {
            SupplierOrderVo supplierOrderVo = new SupplierOrderVo();
            BeanUtils.copyBeanProp(supplierOrderVo, paOrder1);
            //获取商品信息
            PaOrderItem paOrderItem = new PaOrderItem();
            paOrderItem.setOrderId(paOrder1.getOrderId());
            List<PaOrderItem> paOrderItems = paOrderItemMapper.selectPaOrderItemList(paOrderItem);
            List<PaOrderItemBo> paOrderItemBos = BeanUtil.copyToList(paOrderItems, PaOrderItemBo.class);
            //将实际成交价置空
            paOrderItemBos.forEach(paOrderItemBo -> {
                paOrderItemBo.setActualUnintPrice(null);
            });
            supplierOrderVo.setPaOrderItemBos(paOrderItemBos);
            supplierOrderVos.add(supplierOrderVo);
        }
        TableDataInfo<SupplierOrderVo> page = TableDataInfo.build(supplierOrderVos);
        page.setTotal(pageInfo.getTotal());
        return page;
    }

    @Override
    public SupplierOrderVo selectPaOrderByOrderId(Long orderId) {
        SupplierOrderVo supplierOrderVo = new SupplierOrderVo();
        PaOrder paOrder = paOrderMapper.selectPaOrderByOrderId(orderId);
        BeanUtils.copyBeanProp(supplierOrderVo, paOrder);
        //根据paOrder对象的 orderId 去查询paOrderItem商品信息
        PaOrderItem paOrderItem = new PaOrderItem();
        paOrderItem.setOrderId(orderId);
        List<PaOrderItem> paOrderItems = paOrderItemMapper.selectPaOrderItemList(paOrderItem);
        List<PaOrderItemBo> paOrderItemBos = BeanUtil.copyToList(paOrderItems, PaOrderItemBo.class);
        supplierOrderVo.setPaOrderItemBos(paOrderItemBos);
        return supplierOrderVo;
    }

    @Override
    public int updatePaOrder(SupplierUpdateOrderBo supplierOrderBo) {

        //根据orderId查询 paOrderItem
        PaOrderItem paOrderItem = new PaOrderItem();
        paOrderItem.setOrderId(supplierOrderBo.getOrderId());
        List<PaOrderItem> paOrderItems = paOrderItemMapper.selectPaOrderItemList(paOrderItem);
        for (PaOrderItem paOrderItem1 : paOrderItems) {
            paOrderItem1.setUnintPrice(supplierOrderBo.getUnintPrice());
            paOrderItem1.setUpdateTime(DateUtils.getNowDate());
            paOrderItem1.setUpdateBy(String.valueOf(supplierOrderBo.getSupplierId()));
            paOrderItem1.setActualProdCount(supplierOrderBo.getActualProdCount());
            paOrderItemMapper.updatePaOrderItem(paOrderItem1);
        }

        PaOrder paOrder = new PaOrder();
        paOrder.setOrderId(supplierOrderBo.getOrderId());
        paOrder.setUpdateTime(DateUtils.getNowDate());
        //判断BigDecimal 为null
        if (supplierOrderBo.getTotal() != null) {
            paOrder.setTotal(supplierOrderBo.getTotal());
        } else {
            //获取paOrderItemBos里面的 产品个数和产品单价 并相乘
            double prodCount = 0.0;
            for (PaOrderItem orderItem : paOrderItems) {
                double count = Double.parseDouble(orderItem.getProdCount());
                prodCount += count;
            }
            double unintPrice = paOrderItems.stream()
                    .filter(Objects::nonNull) // 过滤掉null值
                    .map(PaOrderItem::getUnintPrice)
                    .filter(Objects::nonNull)
                    .mapToDouble(BigDecimal::doubleValue)
                    .sum();
            // prodCount 和 actualTotal 相乘
            double total = prodCount * unintPrice;
            paOrder.setTotal(new BigDecimal(String.valueOf(total)));
        }
//        paOrder.setActualProductNums(supplierOrderBo.getActualProductNums());
         paOrderMapper.updatePaOrder(paOrder);
        //更新或者插入发货地址
        PaUserAddrOrder paUserAddrOrderUpdate =  supplierOrderBo.getPaUserAddrOrder();
        PaUserAddrOrder param = new PaUserAddrOrder();
        param.setOrderId(supplierOrderBo.getOrderId());
        param.setAddressType("shipping");
        PaUserAddrOrder paUserAddrOrderQuery = addrOrderMapper.selectPaUserAddrOrder(param);
        paUserAddrOrderUpdate.setOrderId(supplierOrderBo.getOrderId());
        paUserAddrOrderUpdate.setUserId(String.valueOf(supplierOrderBo.getSupplierId()));
        paUserAddrOrderUpdate.setUserName(supplierOrderBo.getSupplierName());
        if (paUserAddrOrderQuery != null) {
            paUserAddrOrderUpdate.setAddrOrderId(paUserAddrOrderQuery.getAddrOrderId());
            addrOrderMapper.updatePaUserAddrOrder(paUserAddrOrderUpdate);
        } else {
            addrOrderMapper.insertPaUserAddrOrder(paUserAddrOrderUpdate);
        }
        return 1;
    }
}
